﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Model = AzureGames.Model;


namespace AzureGames.Domain.Models
{
    public class GameDetails
    {
        public GameDetails(Model.GameDefinition gameDefinition, IEnumerable<Model.Statistics.GameStatistics> gameStatistics, IEnumerable<Model.Statistics.UserStatistics> userStatistics)
        {
            Name = gameDefinition.Name;
            ID = gameDefinition.ID;
            GameImageURL = string.Format("/Game/GetResource?resourceID={0}&contentType=image%2Fjpeg", gameDefinition.GameImage);
            Description = gameDefinition.Description;
            ShortDescription = gameDefinition.ShortDescription;

            Version = gameDefinition.Version;

            ReleasedDate =  gameDefinition.CreatedDate.ToShortDateString();

            // initialise
            UserCancelledStatistics = new Dictionary<string, int>();
            UserCreatedStatistics = new Dictionary<string, int>();
            UserCompletedStatistics = new Dictionary<string, int>();

            GameCancelledStatistics = new Dictionary<string, int>();
            GameCreatedStatistics = new Dictionary<string, int>();
            GameCompletedStatistics = new Dictionary<string, int>();

            DaysOfWeek = new string[7];
            DaysOfWeek[0] = "Today";

            for (int i = 1; i < 7; i++)
            {
                string key = DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd");
                DaysOfWeek[i] = DateTime.Now.AddDays(-i).DayOfWeek.ToString();

                UserCreatedStatistics[key] = 0;
                UserCompletedStatistics[key] = 0;
                UserCancelledStatistics[key] = 0;

                GameCancelledStatistics[key] = 0;
                GameCompletedStatistics[key] = 0;
                GameCreatedStatistics[key] = 0;
            }

            foreach (var userStatistic in userStatistics)
            {
                string key = userStatistic.Date.ToString("yyyy-MM-dd");

                if (UserCreatedStatistics.ContainsKey(key))
                    UserCreatedStatistics[key] += userStatistic.CreatedGames;
                else
                    UserCreatedStatistics[key] = userStatistic.CreatedGames;

                if (UserCompletedStatistics.ContainsKey(key))
                    UserCompletedStatistics[key] += userStatistic.CompletedGames;
                else
                    UserCompletedStatistics[key] = userStatistic.CompletedGames;

                if (UserCancelledStatistics.ContainsKey(key))
                    UserCancelledStatistics[key] += userStatistic.CancelledGames;
                else
                    UserCancelledStatistics[key] = userStatistic.CancelledGames;
            }

            foreach (var gameStatistic in gameStatistics)
            {
                string key = gameStatistic.Date.ToString("yyyy-MM-dd");

                if (GameCreatedStatistics.ContainsKey(key))
                    GameCreatedStatistics[key] += gameStatistic.CreatedGames;
                else
                    GameCreatedStatistics[key] = gameStatistic.CreatedGames;

                if (GameCompletedStatistics.ContainsKey(key))
                    GameCompletedStatistics[key] += gameStatistic.CompletedGames;
                else
                    GameCompletedStatistics[key] = gameStatistic.CompletedGames;

                if (GameCancelledStatistics.ContainsKey(key))
                    GameCancelledStatistics[key] += gameStatistic.CancelledGames;
                else
                    GameCancelledStatistics[key] = gameStatistic.CancelledGames;
            }           
        }

        public string Name { get; set; }
        public int ID { get; set; }
        public string GameImageURL { get; set; }
        public string Description { get; set; }
        public string ShortDescription { get; set; }
        public double Version { get; set; }
        public string ReleasedDate { get; set; }

        public string[] DaysOfWeek { get; set; }

        // users
        // each collection represents a line in the graph
        public Dictionary<string, int> UserCreatedStatistics { get; set; }
        public Dictionary<string, int> UserCompletedStatistics { get; set; }
        public Dictionary<string, int> UserCancelledStatistics { get; set; }
        public int MaxUserValue { get; set; }

        // games
        public Dictionary<string, int> GameCreatedStatistics { get; set; }
        public Dictionary<string, int> GameCompletedStatistics { get; set; }
        public Dictionary<string, int> GameCancelledStatistics { get; set; }
        public int MaxGameValue { get; set; }
    }
}
